package org.deltaset.samples.mvc.visit;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.deltaset.meta.annotation.DsFirstResult;
import org.deltaset.meta.annotation.DsLabel;
import org.deltaset.meta.annotation.DsMaxResult;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author dmakariev
 */
@ManagedBean
public class AnimalRepositoryAddonImpl implements AnimalRepositoryAddon {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    protected EntityManager getEntityManager() {
        return entityManager;
    }

    @Transactional
    @Override
    public final Animal new12Records(Integer numberOfNewRecords) {
        for (int i = 10; i < numberOfNewRecords + 10; i++) {
            final Animal animal = new Animal();
            animal.setName("record " + i);
            getEntityManager().merge(animal);
        }
        return new Animal();
    }
    
    @Transactional
    @Override
    public final void nAnimalsForPerson(Integer numberOfNewRecords, Person person) {
        for (int i = 10; i < numberOfNewRecords + 10; i++) {
            final Animal animal = new Animal();
            animal.setName("owned record " + i);
            animal.setOwner(person);
            person.getAnimals().add(animal);           
        }
         getEntityManager().merge(person);
       // return new Animal();
    }

    @Override
    public void doSomething() {
        //do nothing..
    }

    @Transactional
    @Override
    public List<Animal> findByName(@DsFirstResult Integer firstResult, @DsMaxResult Integer maxResult, @DsLabel("named") String name) {
        final List<Animal> founded = getEntityManager().createQuery("FROM " + Animal.class.getName() + " o WHERE o.name LIKE :name ORDER BY o.name").
                setParameter("name", "%" + name + "%").
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<Animal>(founded);
    }
    
    @Override
    public Long countByName(String name) {
        final Long count = getEntityManager().createQuery("SELECT count(o) FROM " + Animal.class.getName() + " o WHERE o.name LIKE :name",Long.class).
                setParameter("name", "%" + name + "%").
                getSingleResult();
        return count;
    }

    @Transactional
    @Override
    public List<Animal> findAllList() {
        final List<Animal> founded = getEntityManager().createQuery("FROM " + Animal.class.getName() + " o ORDER BY o.name").
                getResultList();
        return new ArrayList<Animal>(founded);
    }

    @Override
    public String action(String name, @DsLabel("the_person") Person person, Integer age, Date date, NameTitle nameTitle) {
        return "Hi name=" + name + " !";
    }

    @Override
    public String action(ActionParameter actionParameter) {
        return "Hi ActionParameter name=" + actionParameter.getName() + " !";
    }    
}
